package com.app.ui.fragment

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.Fragment
import com.app.NewsApplication.Companion.if_login
import com.app.R
import com.app.ui.activity.CollectionActivity
import com.app.ui.activity.HistoryActivity
import com.app.ui.activity.LoginActivity
import com.app.util.showToast


class UserFragment : Fragment() {

    lateinit var user:LinearLayout
    lateinit var avatar:de.hdodenhof.circleimageview.CircleImageView
    lateinit var name:TextView
    lateinit var info:TextView
    lateinit var fun_msg:TextView
    lateinit var fun_clt:TextView
    lateinit var fun_his:TextView
    lateinit var fun_dwn:TextView
    lateinit var intent1: Intent
    lateinit var intent2: Intent

    lateinit var svc_avatar:TextView
    lateinit var svc_contact:TextView
    lateinit var svc_logout:TextView
    lateinit var svc_setting:TextView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_user, container, false)
        avatar = view.findViewById(R.id.avatar)
        name = view.findViewById(R.id.user_name)
        info = view.findViewById(R.id.user_info)
        user = view.findViewById(R.id.user)
        fun_msg = view.findViewById(R.id.fun_msg)
        fun_clt = view.findViewById(R.id.fun_clt)
        fun_his = view.findViewById(R.id.fun_his)
        fun_dwn = view.findViewById(R.id.fun_dwn)

        svc_avatar = view.findViewById(R.id.svc_avatar)
        svc_contact = view.findViewById(R.id.svc_contact)
        svc_logout = view.findViewById(R.id.svc_logout)
        svc_setting = view.findViewById(R.id.svc_setting)
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {

        if (name.text!=" "){
            user.setOnClickListener{
                val intent = Intent(activity,LoginActivity::class.java)
                startActivity(intent)
            }
        }

        fun_msg.setOnClickListener {
            "你点击了消息通知".showToast()
        }
        fun_clt.setOnClickListener {
//            Toast.makeText(activity,"你点击了收藏",Toast.LENGTH_SHORT).show()
            val intent1 = Intent(
                this.activity,
                CollectionActivity::class.java
            )
            startActivity(intent1)
        }
        fun_his.setOnClickListener {
//            "你点击了浏览历史".showToast()
            intent2 = Intent(activity,HistoryActivity::class.java)
            startActivity(intent2)
        }
        fun_dwn.setOnClickListener {
            "你点击了下载管理".showToast()
        }
        svc_avatar.setOnClickListener {
            "你点击了修改头像".showToast()
        }
        svc_contact.setOnClickListener {
            AlertDialog.Builder(context)
                .setTitle("联系我们")
                .setMessage("电子邮箱:88888888@qq.com")
                .setPositiveButton("确定",null)
                .setNeutralButton("取消",null)
                .create()
                .show()
        }
        svc_logout.setOnClickListener {
            //改变全局变量的值
            if_login = 0
            "正在注销".showToast()
            Thread.sleep(2000)
            val intent = Intent(activity,LoginActivity::class.java)
            activity!!.finish()
            startActivity(intent)
        }
        svc_setting.setOnClickListener {
            "别催了，正在开发".showToast()
        }

        if(if_login != 0) {
            //设置用户名
            intent1 = this.activity!!.intent
            name.setText(intent1.getStringExtra("user_name"))
        }
        else{
            name.setText("未登录")
        }
        super.onActivityCreated(savedInstanceState)
    }
}